module Unparsed
  def unparsed_attribute attr_name
    module_eval <<-STR, __FILE__, __LINE__ + 1
      def unparsed_#{attr_name}
        JSON.pretty_generate self.#{attr_name} rescue nil
      end

      def unparsed_#{attr_name}=(value)
        self.#{attr_name} = JSON.parse value
      rescue
        self.errors.add(:#{attr_name}, :invalid)
      end
    STR
  end
end
